//@Time:2022/4/9 19:08
//@Author:aFun

package indi.csms_springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import indi.csms_springboot.domain.SCVO;
import indi.csms_springboot.domain.SSFSVO;
import indi.csms_springboot.domain.User;
import indi.csms_springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@RestController
@RequestMapping("/studentController")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/getStuScore")
    public ModelAndView getStuScore(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        User user = (User) request.getSession().getAttribute("user");
        Integer stuId = studentService.findStuId(user);


        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }

        if (rows == null || "".equals(rows)) {
            rows = "5";
        }

        Map<String, String[]> condition = request.getParameterMap();


        IPage<SSFSVO> pb = studentService.studentScore(currentPage, rows, condition, stuId);
        request.setAttribute("pb",pb);
        request.setAttribute("condition",condition);

        modelAndView.setViewName("studentScoreList");
        return modelAndView;
    }


    @RequestMapping("/getStuCourse")
    public ModelAndView getStuCourse(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();

        request.setCharacterEncoding("utf-8");
        User user = (User) request.getSession().getAttribute("user");
        Integer stuId = studentService.findStuId(user);


        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }

        if (rows == null || "".equals(rows)) {
            rows = "5";
        }

        Map<String, String[]> condition = request.getParameterMap();

        IPage<SCVO> pb = studentService.studentCourse(currentPage, rows, condition, stuId);
        request.setAttribute("pb",pb);
        request.setAttribute("condition",condition);

        modelAndView.setViewName("studentCourseList");
        return modelAndView;
    }

    @RequestMapping("/deleteCourse/{ssId}")
    public ModelAndView deleteCourse(HttpServletRequest request,@PathVariable Integer ssId){
        ModelAndView modelAndView=new ModelAndView();
        boolean b = studentService.deleteCourse(ssId);
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/studentController/getStuCourse");
        return modelAndView;
    }

    @RequestMapping("/showCourseToSelect")
    public ModelAndView showCourseToSelect(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        User user = (User) request.getSession().getAttribute("user");
        Integer stuId = studentService.findStuId(user);


        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }

        if (rows == null || "".equals(rows)) {
            rows = "5";
        }

        Map<String, String[]> condition = request.getParameterMap();
        IPage<SCVO> pb = studentService.showCourseToSelect(currentPage, rows, condition, stuId);
        request.setAttribute("pb",pb);
        request.setAttribute("condition",condition);

        modelAndView.setViewName("studentSelect");
        return modelAndView;
    }

    @RequestMapping("/selectCourse/{couId}/{teaId}")
    public ModelAndView selectCourse(HttpServletRequest request,@PathVariable Integer couId,@PathVariable Integer teaId){
        ModelAndView modelAndView=new ModelAndView();

        User user = (User) request.getSession().getAttribute("user");
        Integer stuId = studentService.findStuId(user);

        SCVO scvo = new SCVO();
        scvo.setCouId(couId);
        scvo.setTeaId(teaId);
        scvo.setStuId(stuId);

        boolean b = studentService.selectCourse(scvo);
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/studentController/showCourseToSelect");

        return modelAndView;
    }

}
